﻿// Sheego.Service.X400
// Copyright(C) 2015 Schwab Versand GmbH
//
// This source file is subject to the Microsoft Public License(Ms-PL).
// http://www.opensource.org/licenses/ms-pl.html
// All other rights reserved.
using Sheego.Service.X400.Job.Shared;
using Sheego.Service.X400.Job.Shared.BO;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mail;
using System.Net.Mime;
using System.Text;
using System.Threading.Tasks;

namespace Sheego.Service.X400.Job.Impl
{
    class MailManager : IMailManager
    {
        private IJobConfiguration JobConfiguration;
        private string EnvironmentMachineName = System.Environment.MachineName;

        public MailManager(IJobConfiguration jobConfiguration)
        {
            JobConfiguration = jobConfiguration;
        }

        #region IMailManager Members

        public void Send(IMailMessage Message)
        {

            SmtpClient smtpClient = new SmtpClient();
            //NetworkCredential basicCredential = new NetworkCredential(Username, Password);
            MailMessage message = new MailMessage();
            MailAddress fromAddress = new MailAddress(JobConfiguration.MailSender);

            // setup up the host, increase the timeout to 5 minutes
            smtpClient.Host = JobConfiguration.SmtpServer;
            //smtpClient.UseDefaultCredentials = false;
            //smtpClient.Credentials = basicCredential;
            smtpClient.Timeout = (60 * 5 * 1000);

            message.From = fromAddress;

            message.Subject = String.Format("[{0}] [{1}] - {2} - {3}",
                JobConfiguration.TestMode ? "Test" : "Live", EnvironmentMachineName, Message.SubjectType, Message.SubjectDetail);

            message.IsBodyHtml = false;

            foreach(var Recipient in Message.Recipients)
                message.To.Add(Recipient);

            IList<IDisposable> disposables = new List<IDisposable>();
            foreach (var MsgPart in Message.Attachments)
            {

                Attachment attachment = new Attachment(MsgPart.FilePath, MediaTypeNames.Application.Octet);
                ContentDisposition disposition = attachment.ContentDisposition;
                disposition.CreationDate = File.GetCreationTime(MsgPart.FilePath);
                disposition.ModificationDate = File.GetLastWriteTime(MsgPart.FilePath);
                disposition.ReadDate = File.GetLastAccessTime(MsgPart.FilePath);
                disposition.FileName = MsgPart.Name;
                disposition.Size = new FileInfo(MsgPart.FilePath).Length;
                disposition.DispositionType = DispositionTypeNames.Attachment;
                message.Attachments.Add(attachment);

                disposables.Add(attachment);

            }

            message.Body = Message.Body;
            smtpClient.Send(message);


            //Aufräumen
            disposables.ToList().ForEach(o => o.Dispose());



        }

        #endregion
    }
}
